//
//  HomeRoomDetailVC.m
//  AILock
//
//  Created by 谭亚明 on 2020/7/30.
//  Copyright © 2020 tym. All rights reserved.
//

#import "HomeRoomDetailVC.h"
#import "EquipmentService.h"
#import "GroupShadowTableView.h"
#import "Sizes.h"
#import "LYEmptyViewHeader.h"
#import "OpenLockVC.h"
#import "HomeRoomDetailCell.h"
#import "AddRoomCell.h"

@interface HomeRoomDetailVC ()<GroupShadowTableViewDelegate, GroupShadowTableViewDataSource>

@property (nonatomic, strong) GroupShadowTableView *tableView;

@property(nonatomic,copy) NSMutableArray *equmentArray;
@property(nonatomic,strong) EquipmentService *equmentService;
@end

@implementation HomeRoomDetailVC

-(instancetype)initWithRoomModel:(RoomModel*)roomModel{
    self = [super init];
    if (self) {
        self.roomModel = roomModel;
    }
    return self;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    [self addBackBtnFunc];
    [self addTitle:_roomModel.roomName];
    
    [self setTable];
    self.tableView.ly_emptyView = [TYMEmptyView emptyViewWithImageStr:@"img_empt_equipment"
     titleStr:@"该房间暂无设备"
    detailStr:@""];
}

-(void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self getRoomEquments];
    if (self.navigationController.navigationBarHidden) {
        [self.navigationController setNavigationBarHidden:NO animated:YES];
    }
}

- (void)setTable {
    self.tableView = [[GroupShadowTableView alloc] initWithFrame:CGRectMake(0, 0, ScreenWidth, ScreenHeight-NavigateonHeight-SafeBottomMargin) style:UITableViewStyleGrouped];
    self.tableView.groupShadowDelegate = self;
    self.tableView.groupShadowDataSource = self;
    self.tableView.backgroundColor = viewBGColor;
    self.tableView.sectionFooterHeight = 0;
    _tableView.showSeparator = NO;
    _tableView.separatorStyle = UITableViewCellSeparatorStyleNone;
    [self.view addSubview:_tableView];
}


-(NSMutableArray*)equmentArray{
    if (_equmentArray == nil) {
        _equmentArray = [[NSMutableArray alloc] init];
    }
    return _equmentArray;
}

-(EquipmentService*)equmentService{
    if (_equmentService == nil) {
        _equmentService = [[EquipmentService alloc] init];
    }
    return _equmentService;
}

//获取房间设备
-(void)getRoomEquments{
    TYMWeak(self);
    [self.equmentService getRoomequipments:self.roomModel.familyRoomId completion:^(LMJBaseResponse *respon, NSArray<EquipmentModel *> * _Nonnull array) {
        if (respon.message) {
            [WHToast showMessage:respon.message duration:2 finishHandler:nil];
        }else{
            if (weakself.equmentArray.count) {
                [weakself.equmentArray removeAllObjects];
            }
            [weakself.equmentArray addObjectsFromArray:array];
            [weakself.tableView reloadData];
            if(!weakself.equmentArray.count){
                [weakself.tableView ly_showEmptyView];
            }else{
                [weakself.tableView ly_hideEmptyView];
            }
        }
    }];
}

#pragma mark delegate datasource
- (NSInteger)numberOfSectionsInGroupShadowTableView:(GroupShadowTableView *)tableView {
    return self.equmentArray.count;
}

- (NSInteger)groupShadowTableView:(GroupShadowTableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return 1;
}

- (CGFloat)groupShadowTableView:(GroupShadowTableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
    return [HomeRoomDetailCell cellForHeight];
}

- (UITableViewCell *)groupShadowTableView:(GroupShadowTableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    HomeRoomDetailCell *cell = [tableView dequeueReusableCellWithIdentifier:[HomeRoomDetailCell identifier]];
    if (!cell) {
        cell = [[HomeRoomDetailCell alloc] initWithStyle:UITableViewCellStyleValue1 reuseIdentifier:[AddRoomCell identifier]];
    }
    cell.equipModel =self.equmentArray[indexPath.section];
    return cell;
}

-(void)groupShadowTableView:(GroupShadowTableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    
   OpenLockVC *openVC = [[OpenLockVC alloc] initWithEquipment:_equmentArray[indexPath.section]];
   [self.navigationController pushViewController:openVC animated:YES];
}
@end
